﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SaiLing.Common;
using SaiLing.SingleSignOn.Application.Service.Interfaces.System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using SaiLing.SingleSignOn.Application.Core.Controllers;
using SaiLing.SingleSignOn.Application.Core.Params;
using SaiLing.SingleSignOn.Application.Service;
using SaiLing.Web;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SaiLing.SingleSignOn.WebApi.Controllers
{
    public class ApplicationController : BaseController
    {
        private IApplicationService ApplicationService { get; set; }

        public ApplicationController(IApplicationService applicationService)
        {
            this.ApplicationService = applicationService;
        }

        [HttpGet]
        public async Task<ApiJsonResultData> GetKey()
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                await ServiceFactory.LogService.LogAsync("添加功能模块", $"模块名称:", LogUser);
                x.Data = new { appkey = await Domain.Entities.System.Application.GenerateKey(), appsecret = await Domain.Entities.System.Application.GenerateSecret() };
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> GetByKey(Guid id)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                x.Data = await ApplicationService.GetByKey(id);
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> List(string name, string appkey)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await ApplicationService.List(name, appkey);
                x.Data = list;
            });
        }

        [HttpGet]
        public async Task<ApiJsonResultData> PageList(int pageIndex, int pageSize, string name)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var list = await ApplicationService.PageList(pageIndex, pageSize, name);
                x.Data = list;
            });
        }

        public async Task<ApiJsonResultData> Create([FromForm]Domain.Entities.System.Application model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var result = await ApplicationService.Create(model);
                x.AddMessage("创建成功");
                x.Data = result;
                x.RedirectUrl = @return;
            });
        }

        public async Task<ApiJsonResultData> Edit([FromForm]Domain.Entities.System.Application model, string @return)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                var result = await ApplicationService.Edit(model);
                x.AddMessage("修改成功");
                x.Data = result;
                x.RedirectUrl = @return;
            });
        }

        [HttpPost]
        public async Task<ApiJsonResultData> Delete([FromBody]List<RemoveParam> model)
        {
            return await new ApiJsonResultData().RunWithTryAsync(async x =>
            {
                if (model != null && model.Any())
                {
                    var list = new List<SaiLing.SingleSignOn.Domain.Entities.System.Application>();
                    model.ToList().ForEach(f =>
                    {
                        list.Add(new Domain.Entities.System.Application { Id = f.Id });
                    });
                    var rulst = await ApplicationService.Delete(list.ToArray());
                    x.ReloadPage = true;
                    x.Data = rulst;
                    x.AddMessage("删除成功");
                }
                else
                    x.AddErrorMessage("至少选择一条数据");
            });
        }
    }
}
